﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Services.DataCache;
using Enter.Services.Mail;
using Enter.Mvc.Base.Controllers;
using System.Web.Mvc;
using Enter.Entity;
using Enter.Mvc.Mall.Model.Product;
using System.Data;
using Enter.Mvc.Mall.Model;
using System.Dynamic;
using Enter.Mvc.Mall.Model.User;
using Enter.Mvc.Mall.Model.Order;
using Enter.Config;

namespace Enter.Mvc.Mall.Controllers
{
    public class DetailController: BaseControllers
    {
        private readonly ICache icache;
        private readonly IMail imail;

        public DetailController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
            T201_UserEntity UserLogin = imail.GetUserByName("tongli");

            UserInfo user = new UserInfo();
            user.UserID = UserLogin.UIID;
            user.UserName = UserLogin.UName;
            user.ShopNo = UserLogin.ShopNo;
            user.UserType = (UserInfo.EnumUserType)(int)UserLogin.UserType;
            UserInfo.CurrentUser = user;
        }
        #region 商品详情
        public ActionResult item(int id)
        {
            ProductModel model = null;
            T101_GoodsEntity entity = imail.GetProduct(id);
          if (entity != null)
          {
               model = new ProductModel();
               model.GID = entity.GID;
               model.No = entity.GNo;
               model.WeekSaleCount = entity.WeekSaleCount;
               model.CommentScore = entity.GGCommentScore;
               model.CommentCount = entity.CommentCount;
               model.SalePRICE = entity.GSalePRICE;
               model.ShopNO = entity.GShopNO;
               model.Title = entity.GTitle;
               model.CollectCount = entity.CollectCount;
           

              // DataTable dtprop = imail.GetProdoudProp(model.No);
              // DataTable dtbrand = imail.GetProdoudBrand(model.No);
              // List<Property_Value> list = new List<Property_Value>();
              // foreach (DataRow dr in dtprop.Rows)
              //{
                  
              //     string pNO=dr["PropertyNO"].ToString();
              //     string pName = dr["PName"].ToString();
              //     string vNo = dr["PropertyValue"].ToString();
              //     string vName = dr["VName"].ToString();
              //     Property_Value pv= list.Find(p => p.PropertyName.PNo == pNO);
              //     if (pv != null)
              //     {
              //         PropertyValueModel vmodel = new PropertyValueModel();
              //         vmodel.VNO = vNo;
              //         vmodel.VName = vName;

              //         pv.PropertyValues.Add(vmodel);
              //     }
              //     else {
              //         Property_Value pvmodel = new Property_Value();
              //         PropertyModel pmodel = new PropertyModel();
              //         pmodel.PNo=pNO;
              //         pmodel.PName = pName;
              //         pvmodel.PropertyName = pmodel;
              //         List<PropertyValueModel> vlist = new List<PropertyValueModel>();
              //         PropertyValueModel vmodel = new PropertyValueModel();
              //         vmodel.VNO = vNo;
              //         vmodel.VName = vName;
              //         vlist.Add(vmodel);
              //         pvmodel.PropertyValues = vlist;
              //         list.Add(pvmodel);
              //     }
              //}

          }
          return View(model);
        }

        public ActionResult ProductImg(string NO)
        {

            List<T104_GoodImgesEntity> ImgesList = imail.GetProductInfoImg(NO);
            List<ProductImage> imges = null;
            if (ImgesList != null && ImgesList.Count > 0)
            {
                imges = new List<ProductImage>();
                foreach (T104_GoodImgesEntity entity in ImgesList)
                {
                    ProductImage img = new ProductImage();
                    img.ImgUrl =Config.ImgPathConfig.productInfoServerPath+ entity.ImgUrl;
                    img.SmallImgUrl = Config.ImgPathConfig.productInfoServerPath + entity.SmallImgUrl;
                    imges.Add(img);
                }
            }

            return View(imges);
        }
        public ActionResult ProductItem(string NO)
        {
            List<T103_Goods_ItemEntity> Colorlist = imail.getProductColorListByGNO(NO);
            List<ColorModel> colors = null;
            if (Colorlist != null && Colorlist.Count>0)
            {
                colors = new List<ColorModel>();
                foreach (T103_Goods_ItemEntity entity in Colorlist)
                {
                    ColorModel color = new ColorModel();
                    color.CNo = entity.ColorNO;
                    color.CName = entity.ColorName;
                    color.imgUrl = entity.SmallImgUrl;
                    colors.Add(color);
                }
            }
            List<T103_Goods_ItemEntity> Sizelist = imail.getProductSizeListByGNO(NO);
            List<SizeModel> sizes = null;
            if (Sizelist != null && Sizelist.Count > 0)
            {
                sizes = new List<SizeModel>();
                foreach (T103_Goods_ItemEntity entity in Sizelist)
                {
                    SizeModel size = new SizeModel();
                    size.SizeNO = entity.SizeNO;
                    size.SizeName = entity.SizeName;
                    sizes.Add(size);
                }
            }
            IDictionary<string, object> obj = new Dictionary<string, object>();
            obj.Add("color", colors);
            obj.Add("size", sizes);
            return View(obj);
        }

        public ActionResult GetStock(string GNO, string SizeNo, string ColorNo)
        {
           

            try
            {

                T103_Goods_ItemEntity item = imail.GetProductItem(GNO, ColorNo, SizeNo);
                if (item != null)
                {
                  

                    var obj = new
                    {
                        ResultCode = "00",
                        itemID = item.ID,
                        itemNo = item.PGNo,
                        Stock = item.GSTOCKS,
                        price = item.PSalePRICE
                    };
                    return Json(obj);
                }
                else {
                    var obj = new
                    {
                        ResultCode = "01",
                        ResultMsg = "未找到库存"
                    };
                    return Json(obj);
                }

            }
            catch (Exception ex)
            {
                var obj = new
                {
                    ResultCode = "01",
                    ResultMsg = ex.Message
                };
                return Json(obj);
            }
        }

        public ActionResult ProductProp(string NO)
        {
            DataTable dtprop = imail.GetProdoudProp(NO);

            List<Property_Value> list = new List<Property_Value>();
            DataTable dtbrand = imail.GetProdoudBrand(NO);

            foreach (DataRow dr in dtbrand.Rows)
            {


                string vNO = dr["PropertyValue"].ToString();
                string vName = dr["BName"].ToString();

                Property_Value pvmodel = new Property_Value();
                PropertyModel pmodel = new PropertyModel();
                pmodel.PNo = "brand";
                pmodel.PName = "品牌";
                pvmodel.PropertyName = pmodel;
                List<PropertyValueModel> vlist = new List<PropertyValueModel>();
                PropertyValueModel vmodel = new PropertyValueModel();
                vmodel.VNO = vNO;
                vmodel.VName = vName;
                vlist.Add(vmodel);
                pvmodel.PropertyValues = vlist;
                list.Add(pvmodel);
            }

            foreach (DataRow dr in dtprop.Rows)
            {

                string pNO = dr["PropertyNO"].ToString();
                string pName = dr["PName"].ToString();
                string vNo = dr["PropertyValue"].ToString();
                string vName = dr["VName"].ToString();
                Property_Value pv = list.Find(p => p.PropertyName.PNo == pNO);
                if (pv != null)
                {
                    PropertyValueModel vmodel = new PropertyValueModel();
                    vmodel.VNO = vNo;
                    vmodel.VName = vName;

                    pv.PropertyValues.Add(vmodel);
                }
                else
                {
                    Property_Value pvmodel = new Property_Value();
                    PropertyModel pmodel = new PropertyModel();
                    pmodel.PNo = pNO;
                    pmodel.PName = pName;
                    pvmodel.PropertyName = pmodel;
                    List<PropertyValueModel> vlist = new List<PropertyValueModel>();
                    PropertyValueModel vmodel = new PropertyValueModel();
                    vmodel.VNO = vNo;
                    vmodel.VName = vName;
                    vlist.Add(vmodel);
                    pvmodel.PropertyValues = vlist;
                    list.Add(pvmodel);
                }
            }
            return View(list);
        }

#endregion

        #region 订单详情
        public ActionResult orderDetail(int bizOrderId)
        {

            IDictionary<string, object> test = new Dictionary<string, object>();
            OrderModel model = new OrderModel();
            T301_Order_MainEntity entity = imail.GetOrderInfo(bizOrderId,UserInfo.CurrentUser.UserID);
            if (entity != null)
            {
               
                model.ID = entity.OMID;
               
                model.OrderNO = entity.OMOrderNO;
                model.AMOUNT = entity.OAMOUNT;
                model.DisAmount = entity.ODisAmount;
                model.GoodAmount = entity.OGoodAmount;
                model.ISShopComment = entity.ISShopComment;
                model.ISUserComment = entity.ISUserComment;
                model.SNO = entity.SNO;
                model.ShopName = entity.ShopName;
                model.STATE = entity.OSTATE;
                model.YunFei = entity.OYunFei;
                model.AddTime = entity.OAddTime.ToString("yyyy-MM-dd HH:mm:ss");
                model.items = new List<Order_ItemsModel>();
                foreach (T302_Order_ItemsEntity item in entity.items)
                {
                    Order_ItemsModel itemmodel = new Order_ItemsModel();
                    itemmodel.ID = item.ID;
                    itemmodel.GID = item.GID;

                    itemmodel.GItemID = item.GItemID;
                    itemmodel.ColorName = item.ColorName;
                    itemmodel.SizeName = item.SizeName;
                    if (string.IsNullOrEmpty(item.ColorName))
                    {
                        itemmodel.ColorName = "";
                    }
                    if (string.IsNullOrEmpty(item.SizeName))
                    {
                        itemmodel.SizeName = "";
                    }
                    itemmodel.ProdTitle = item.ProdTitle;
                    itemmodel.OPRICE = item.OPRICE;
                    itemmodel.ONUM = item.ONUM;
                    itemmodel.ImgUrl = ImgPathConfig.productListServerPath + item.ImgUrl;
                    itemmodel.SmallImgUrl = ImgPathConfig.productInfoServerPath + item.SmallImgUrl;
                    model.items.Add(itemmodel);
                }
            }
          T401_ShopEntity shop=imail.GetShopBySNO(model.SNO);
          if (shop != null)
          { 
          
          }
            test.Add("Orderinfo", model);
          

            return View(test);
        }
        #endregion
    }
}
